# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=postgresql
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=15.3
pkgrel=2
pkgdesc="Libraries for use with PostgreSQL (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.postgresql.org/"
license=('spdx:PostgreSQL')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-tcl"
         #"${MINGW_PACKAGE_PREFIX}-readline"
         #"${MINGW_PACKAGE_PREFIX}-wineditline"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd"
         "winpty")
source=("https://ftp.postgresql.org/pub/source/v${pkgver}/postgresql-${pkgver}.tar.bz2"
        postgresql-12.0-mingw-link.patch
        postgresql-9.5.1-pl-perl.patch
        postgresql-9.5.1-pl-python.patch
        postgresql-9.4.1-pl-tcl.patch
        postgresql-9.4.1-mingw-enable-readline.patch
        postgresql-13.1-disable-wsa-invalid-event-static-assert.patch
        postgresql-13.1-pgevent-def.patch
        postgresql-14.0-use-mingw-setjmp-on-ucrt.patch
        postgresql-15.1-clang-16.patch)
sha256sums=('ffc7d4891f00ffbf5c3f4eab7fbbced8460b8c0ee63c5a5167133b9e6599d932'
            '607217b422349770d25af20f88e4a7925e68bb934232dff368c2ee59f24249a4'
            '99e6c8b9d58a9615c7bb1e7471b8e9c074add7bffc7d2cf742bafa38e9654e1f'
            'ced0ccf978eefc1ad0641803c9759b652e588eb552ec9179cd9dfc9d94ecf499'
            'b12c4e493845b3ead8950bbc26b1cc7bd4fb0dc3313fea4f7b281866d18c0402'
            'cd52d56833c60afdce98d91eece73c11978762671e36be7bd9c31a1ea0a0282d'
            '72c14a78eeafdd3c9a13c3e124b1941b5da090488c7bd73f08b3cd78bacd07d5'
            'ffaecbe5a38877728bddbf307b379b39c645dd1ffe53aa8d84dfa96807494764'
            'e65f664644faa5cbd392c575a13bace31799e19d16be64cee362ba0e4a361c23'
            '77876e4f8fd2500db2a1815628bd8d8bbd825563da19f8ea70ac9f9b6659414c')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${srcdir}/postgresql-${pkgver}
  apply_patch_with_msg \
    postgresql-12.0-mingw-link.patch \
    postgresql-9.5.1-pl-perl.patch \
    postgresql-9.5.1-pl-python.patch \
    postgresql-9.4.1-pl-tcl.patch \
    postgresql-13.1-disable-wsa-invalid-event-static-assert.patch \
    postgresql-13.1-pgevent-def.patch \
    postgresql-14.0-use-mingw-setjmp-on-ucrt.patch \
    postgresql-15.1-clang-16.patch
  #patch -p1 -i ${srcdir}/postgresql-9.4.1-mingw-enable-readline.patch

  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  export PYTHON=${MINGW_PREFIX}/bin/python

  ../postgresql-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-thread-safety \
    --enable-integer-datetimes \
    --enable-nls \
    --disable-rpath \
    --with-icu \
    --with-ldap \
    --with-libxml \
    --with-libxslt \
    --with-lz4 \
    --with-ssl=openssl \
    --with-python \
    --with-zstd \
    --without-perl \
    --with-tcl \
    --with-tclconfig=${MINGW_PREFIX}/lib
    #--with-libedit-preferred

  #for dir in src/interfaces src/bin/pg_config src/bin/psql src/bin/pg_dump; do
  #  make -C ${dir}
  #done

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/"{bin,include,lib}

  make DESTDIR="${pkgdir}" install
  # install libs

  #for dir in src/interfaces src/bin/pg_config src/bin/psql src/bin/pg_dump; do
  #  make -C ${dir} DESTDIR="${pkgdir}" install
  #done

  # Move dll's to bin directory
  mv "${pkgdir}${MINGW_PREFIX}/lib/"*.dll "${pkgdir}${MINGW_PREFIX}/bin/"

  # Rename the .a files to .dll.a as they're actually import libraries and not static libraries
  #for implib in "${pkgdir}${MINGW_PREFIX}/lib/"*.a; do
  #  mv $implib ${implib/.a/.dll.a}
  #done

  # these headers are needed by the not-so-public headers of the interfaces
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/include/{libpq,postgresql/internal/libpq}
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/c.h "${pkgdir}${MINGW_PREFIX}/include/postgresql/internal/"
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/port.h "${pkgdir}${MINGW_PREFIX}/include/postgresql/internal/"
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/postgres_fe.h "${pkgdir}${MINGW_PREFIX}/include/postgresql/internal/"
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/libpq/pqcomm.h "${pkgdir}${MINGW_PREFIX}/include/postgresql/internal/libpq/"

  # these headers are needed by the public headers of the interfaces
  #  install -m644 pg_config.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  install -m644 pg_config_os.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  install -m644 pg_config_ext.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  cd "${srcdir}/postgresql-$pkgver/src/include"
  #  install -m644 postgres_ext.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  install -m644 libpq/libpq-fs.h "${pkgdir}${MINGW_PREFIX}/include/libpq/"
  #  install -m644 pg_config_manual.h "${pkgdir}${MINGW_PREFIX}/include/"

  # Use winpty-git script to invoke utilities. Please don't move this into a patch as
  # hopefully one day we won't need this hack.
  for f in clusterdb createdb createuser dropdb dropuser initdb pg_basebackup pg_dump pg_dumpall pg_receivewal pg_restore psql reindexdb vacuumdb; do
    mv "${pkgdir}"${MINGW_PREFIX}/bin/${f}.exe "${pkgdir}"${MINGW_PREFIX}/bin/${f}_exe
    cat > "${pkgdir}${MINGW_PREFIX}/bin/${f}" <<END
#!/usr/bin/env bash

EXE="\$( dirname \${BASH_SOURCE[0]} )/${f}.exe"

if [ -t 0 -a -t 1 -a -x /usr/bin/winpty ]; then
  /usr/bin/winpty \$EXE "\$@"
else
  exec \$EXE "\$@"
fi
END
    chmod +x "${pkgdir}${MINGW_PREFIX}/bin/${f}"
    mv "${pkgdir}"${MINGW_PREFIX}/bin/${f}_exe "${pkgdir}"${MINGW_PREFIX}/bin/${f}.exe
  done
}
